#include"miscdataWidget.h"

MiscdataWidget::MiscdataWidget(QWidget* p):QWidget(p),ui(new Ui::MiscdataWidget),filePath("")
{
    ui->setupUi(this);
    setInputMask();
}

MiscdataWidget::~MiscdataWidget()
{
    delete ui;
}

void MiscdataWidget::setInputMask()
{

}

MiscData MiscdataWidget::getMiscdataFromUI()
{
    MiscData e;

    e.energy_recover_time = (qint32)ui->lineEdit_energy_recover_time->text().toInt();
    e.ammo_recover_time = (qint32)ui->lineEdit_ammo_recover_time->text().toInt();
    e.free_speak_times = (qint8)ui->lineEdit_free_speak_times->text().toInt();//qDebug()<<(qint8)(ui->lineEdit_free_speak_times->text().toInt());
    e.treasure_compose_time = (qint32)ui->lineEdit_treasure_compose_time->text().toInt();
    e.displayed_enemies = (qint8)ui->lineEdit_displayed_enemies->text().toInt();
    e.displayed_expeditions = (qint8)ui->lineEdit_displayed_expeditions->text().toInt();
    e.displayed_messages = (qint8)ui->lineEdit_displayed_messages->text().toInt();
    e.strengthen_needed_money = (qint32)ui->lineEdit_strenghthen_needed_cash->text().toInt();
    e.equip_dropped_probability = (qint8)ui->lineEdit_equip_dropped_probability->text().toInt();
    e.get_treasure_probability = (qint8)ui->lineEdit_get_treasure_probability->text().toInt();
    e.day_invites = (qint32)ui->lineEdit_day_invites->text().toInt();
    e.day_messages_to_leave = (qint32)ui->lineEdit_day_messages_to_leave->text().toInt();
    e.generals_upper = (qint8)ui->lineEdit_general_upper->text().toInt();
    e.weapons_upper = (qint8)ui->lineEdit_weapon_upper->text().toInt();
    e.armors_upper = (qint8)ui->lineEdit_armor_upper->text().toInt();
    e.horses_upper = (qint8)ui->lineEdit_horses_upper->text().toInt();
    e.treasure_upper = (qint8)ui->lineEdit_treasure_upper->text().toInt();

    e.cnt_player = 10;

    e.level_exp_player = new qint32[e.cnt_player];
    e.energy_upper = new qint32[e.cnt_player];
    e.ammo_upper = new qint8[e.cnt_player];
    e.team_upper = new qint8[e.cnt_player];
    e.friend_upper = new qint8[e.cnt_player];
    for(int i=0;i<e.cnt_player;i++)
    {
        if(ui->tableWidget->item(i,0))
            e.level_exp_player[i] = (qint32) ui->tableWidget->item(i,0)->text().toInt();
        else
            e.level_exp_player[i] = 0;

        if(ui->tableWidget->item(i,2))
            e.energy_upper[i] = (qint32) ui->tableWidget->item(i,2)->text().toInt();
        else
            e.energy_upper[i] = 0;

        if(ui->tableWidget->item(i,3))
            e.ammo_upper[i] = (qint8) ui->tableWidget->item(i,3)->text().toInt();
        else
            e.ammo_upper[i] = 0;

        if(ui->tableWidget->item(i,4))
            e.team_upper[i] = (qint8) ui->tableWidget->item(i,4)->text().toInt();
        else
            e.team_upper[i] = 0;

        if(ui->tableWidget->item(i,5))
            e.friend_upper[i] = (qint8) ui->tableWidget->item(i,5)->text().toInt();
        else
            e.friend_upper[i] = 0;
    }

    e.cnt_other = 5;

    e.level_exp_other = new qint32[e.cnt_other];
    for(int i=0;i<e.cnt_other;i++)
        e.level_exp_other[i] = (qint32)ui->tableWidget->item(i,1)->text().toInt();

    return e;
}

void MiscdataWidget::displayMiscdataToUI(MiscData &s)
{
    ui->lineEdit_energy_recover_time->setText(QString::number(s.energy_recover_time));
    ui->lineEdit_ammo_recover_time->setText(QString::number(s.ammo_recover_time));
    ui->lineEdit_free_speak_times->setText(QString::number(s.free_speak_times));
    ui->lineEdit_treasure_compose_time->setText(QString::number(s.treasure_compose_time));
    ui->lineEdit_displayed_enemies->setText(QString::number(s.displayed_enemies));
    ui->lineEdit_displayed_expeditions->setText(QString::number(s.displayed_expeditions));
    ui->lineEdit_displayed_messages->setText(QString::number(s.displayed_messages));
    ui->lineEdit_strenghthen_needed_cash->setText(QString::number(s.strengthen_needed_money));
    ui->lineEdit_equip_dropped_probability->setText(QString::number(s.equip_dropped_probability));
    ui->lineEdit_get_treasure_probability->setText(QString::number(s.get_treasure_probability));
    ui->lineEdit_day_invites->setText(QString::number(s.day_invites));
    ui->lineEdit_day_messages_to_leave->setText(QString::number(s.day_messages_to_leave));
    ui->lineEdit_general_upper->setText(QString::number(s.generals_upper));
    ui->lineEdit_weapon_upper->setText(QString::number(s.weapons_upper));
    ui->lineEdit_armor_upper->setText(QString::number(s.armors_upper));
    ui->lineEdit_horses_upper->setText(QString::number(s.horses_upper));
    ui->lineEdit_treasure_upper->setText(QString::number(s.treasure_upper));

    for(int i=0;i<s.cnt_player;i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(s.level_exp_player[i])));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString::number(s.energy_upper[i])));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString::number(s.ammo_upper[i])));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(s.team_upper[i])));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(s.friend_upper[i])));
    }
    for(int i=0;i<s.cnt_other;i++)
    {
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(s.level_exp_other[i])));
    }
}

MiscData MiscdataWidget::readMiscdataFromFile()
{
    MiscData s;
    QFile f(filePath);
    if(f.exists())
    {
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        int magicTemp;
        int versionTemp;
        in>>magicTemp;
        in>>versionTemp;
        in>>s;
//        cout<<"magic:"<<magicTemp<<endl;
//        cout<<"version:"<<versionTemp<<endl;
//        cout<<s;
        displayMiscdataToUI(s);
        f.close();
    }else
    {
        f.open(QIODevice::WriteOnly);
        f.close();
    }

    return s;
}

bool MiscdataWidget::saveMiscdataToFile()
{
    MiscData s;
    s = getMiscdataFromUI();
    QFile f(filePath);
    f.open(QIODevice::WriteOnly);
    QDataStream out(&f);
    out<<MiscData::magic;
    out<<MiscData::version;
    out<<s;
    f.close();
    return true;
}
